<?php
namespace control;

/*
*待办事项类
*/
class backlogClass extends adIndexClass
{
    private const title = '待办事项';

    /**
     * 字段初始化
     */
    public function fieldInit($fun): array
    {
        $tr = [
            'text'          => [
                'name'  => L('person.table.nei_rong'),
                'width' => 1200,
            ],
            'startDay'  => [
                'name'  => L('person.table.ti_shi_ri_qi'),
                'width' => 200,
                'sort' => 1,
            ],

        ];
        switch ($fun) {
            case 'processedSearch':
                $tr = array_merge($tr,
                    [
                        'updateTime'         =>
                            [
                                'name'  => L('person.table.chu_li_shi_jian'),
                                'width' => 200,
                                'sort' => 1,
                            ]
                    ]
                );
                break;
        }
        return $tr;
    }

    /*列表页-查询条件*/
    public function homeOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->tableShow([
            [
                [
                    L('person.table.nei_rong') => text(array("name" => "text")).
                        hidden(["name" => "workFlow", 'value'=> '待处理']),
                    L('person.table.reminderDate')  => text(["name" => "date", 'laydate' => [ 'range' => '~', 'type'=> 'date'] ]),
                ]
            ]
        ],1);
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    /**
     * 行政人事-待办事项-查询条件
     *
     * @param string $workFlow
     * @return string
     */
    public function home()
    {
        $html = $this->homeOptions(false);
        //控件
        $button = "";
        /*if ($this->post('workFlow') == '待处理') {
            $button = "<span onclick=\"layer('backlog','layerModel',{type:'read'})\" class='spanButton'>标记为已读</span>\n";
        }*/
        //返回
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }

    /*列表页-查询条件*/
    public function processedOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->tableShow([
            [
                [
                    L('person.table.nei_rong') => text(array("name" => "text")).
                        hidden(["name" => "workFlow", 'value'=> '已完成']),
                ],
                [
                    L('person.table.processingTime')  => text(["name" => "date", "class" => "width300", 'laydate' => [ 'range' => '~', 'type'=> 'datetime']]),
                ]
            ]
        ],1);
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    /**
     * 已处理
     *
     */
    public function processed()
    {
        $html = $this->processedOptions(false);
        //控件
        $button = "";
        //返回
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }
    public function processedSearch()
    {
        return $this->homeSearch();
    }


    /**
     * 查询结果
     *
     * @return string
     */
    public function homeSearch()
    {
        //like查询数组
        $like = array(
            "text" => $this->post('text'),//待办事项说明
        );
        //全等查询数组
        $equal = array(
            "workFlow" => $this->post('workFlow'),//办理状态（待处理/已完成）
        );
        //设置查询变量为空
        $and = " and stid = '".self::$stid."' ";
        if (!empty($this->post('date'))) {
            if ($this->post('workFlow') == '待处理') {
                $and .= self::timeComparison($this->post('date'), 'date', 'date');

            } else {
                $and .= self::timeComparison($this->post('time'), 'time', 'updateTime');
            }
        }
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'time',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        if ($this->post('workFlow') == '待处理') {
            $orderBy['orderBy'] = "FIELD(urging,'是','否'),".$orderBy['orderBy'];
        }
        //获取查询结果
        $where = search(array("like" => $like,"=" => $equal)).$and;
        $page = $this->page(["table" => "backlog","where" => $where, "order" => $orderBy['orderBy']]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        /*if ($this->post('workFlow') == '待处理') {
            //表格控件区
            $liButton = array(
                $this->checkAll("listForm", "listId")
            );
        }*/
        //表格控件区
        $liButton = array('');
        //获取记录
        foreach ($page['array'] as $array) {
            if ($array['workFlow'] == '待处理') {
                if ($array['urging'] =='是'){
                    $array['text']="<span style='color: red'>".L('person.table.urge')."</span>".$array['text'];
                }
            }
            $info = query('audit', " id ='$array[typeId]'");
            if (!empty($array['targetCla']) && !empty($array['targetFun']) && empty($info['id'])) {
                $click= "newIframe('{$array['type']}详情','".root."control/adIndex.html?cla={$array['targetCla']}&fun={$array['targetFun']}&id={$array['typeId']}','','',true)";
            }else{
                $click= "newIframe('{$array['type']}详情','".root."control/adIndex.html?cla={$info['targetCla']}&fun={$info['targetFun']}&id={$info['targetId']}','','',true)";
            }
            //列表控件
            $dominate = array(
                "detail" => ["title" => self::title . "详情", "click" => "preValidationCoid(this,'{$array['coid']}')", "other" => " data-fun=\"$click\" "],
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $button = '';
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
        ));
        $json['html'] = $page['hint'].$button."<form name='listForm'>".$li."</form>".$page['button'].clear;
        return json_encode($json, 320);
    }


    /**
     * 弹窗
     * @return bool|string
     */
    public function layerModel(): bool|string
    {
        //准备数据
        $html = $this->layerEdit(array("table" => array(
            array(
                must . L('person.table.tip')."：" => L('person.table.sfbjzxdbxxwywc'),
            )
        )));

        return $this->layer(array("title" => L('person.table.biao_ji'), "height" => 180, "html" => $html, 'formTwo' => 'listForm'));
    }

    /**
     *弹窗编辑
     */
    public function layerModelEdit(): bool|string
    {
        $listId = $this->post('listId');
        if (empty($listId) || !is_array($listId)) {
            $json['warn'] = L('person.table.qxzxybjwywcddbxx');
        } else {
            $json['warn'] = update(array("table" => "backlog", "field" => array('workFlow' => '已完成'), "where" => "id IN ('" . implode("','", $listId) . "') and workFlow = '待处理'"));
        }
        if (in_array($json['warn'], array("新增成功", "更新成功"))) {
            //添加日志
            $text = str_replace(['{{staffName}}'],[$this->control['name']],L('person.table.markDone'));
            $this->logText(array("type" => "backlog", "text" => $text));
            //返回
            $_SESSION['warn'] = L('person.table.bjcg');
            $json['warn'] = 2;
        }
        return json_encode($json, 320);
    }
}
